package com.framework.base.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * @Copyright @ 2011 All right reserved
 * @author gyd
 * @description  引入JQuery验证
 */
public class JQueryValidate extends TagSupport {
	private static final long serialVersionUID = -5275701732396761003L;
	private String formName;
	
	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		StringBuffer contentsb=new StringBuffer("<script type=\"text/javascript\">").append("\n");
		contentsb.append("$(document).ready(function(){").append("\n");
//		contentsb.append(" $(\"#")assetInfoForm"\").validate({").append("\n");
		contentsb.append("$(\"#").append(getFormName()).append("\").validate({");
		contentsb.append("  success: function(label) {").append("\n");
		contentsb.append("   label.html(\"&nbsp;\").addClass(\"checked\");").append("\n");
		contentsb.append("},").append("\n");
		contentsb.append("errorPlacement: function(error, element) {").append("\n");
		contentsb.append("if ( element.is(\":radio\") )").append("\n");
		contentsb.append("error.appendTo( element.parent() )").append("\n");
		contentsb.append("else").append("\n");
		contentsb.append("error.appendTo( element.parent() )").append("\n");
		contentsb.append(";}").append("\n");
		contentsb.append("	});").append("\n");
		contentsb.append("})").append("\n");
		contentsb.append("</script>");
		try {
			out.write(contentsb.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw new JspException("错误信息: Jquery验证出错");
		}
		return super.doStartTag();
	}
}
